package com.sicau.socialpracticesystem.controller;


import com.sicau.socialpracticesystem.common.constant.UrlConstants;
import com.sicau.socialpracticesystem.common.returning.ResultData;
import com.sicau.socialpracticesystem.config.aop.ValidateAspect;
import com.sicau.socialpracticesystem.entity.param.pointexchange.PointsExchangeAddParam;
import com.sicau.socialpracticesystem.entity.param.pointexchange.PointsExchangeQueryParam;
import com.sicau.socialpracticesystem.entity.po.PointsExchange;
import com.sicau.socialpracticesystem.entity.vo.PageDataVO;
import com.sicau.socialpracticesystem.service.PointsExchangeService;
import com.sicau.socialpracticesystem.util.PageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * <p>
 * 积分使用记录表 前端控制器
 * </p>
 *
 * @author sxt && ttxxi
 * @since 2021-04-26
 */
@RestController
@PreAuthorize("isAuthenticated()")
@RequestMapping(UrlConstants.POINTS_EXCHANGE_PREFIX)
@Slf4j
@Api(tags = "积分使用记录相关")
public class PointsExchangeController {

    @Resource
    PointsExchangeService pointsExchangeService;

    @PostMapping
    @ApiOperation(value = "兑换奖品，后端已做数量判断，但是前端的同学在请求时还是要判断一下数量是否合法")
    @ValidateAspect.ParamValidate
    public ResultData<Boolean> addPointsExchange(@RequestBody @Valid PointsExchangeAddParam pointsExchangeAddParam, BindingResult bindingResult){
        return new ResultData<Boolean>().OK().setObj(pointsExchangeService.addPointsExchange(pointsExchangeAddParam));
    }

    @GetMapping("/page")
    @ApiOperation(value = "根据用户id分页查询兑换记录")
    @ValidateAspect.ParamValidate
    public ResultData<PageDataVO<PointsExchange>> queryPage(@Valid PointsExchangeQueryParam pointsExchangeQueryParam, BindingResult bindingResult){
        return new ResultData<PageDataVO<PointsExchange>>().OK().setObj(PageUtil.convert(pointsExchangeService.queryPage(pointsExchangeQueryParam)));
    }

}

